package com.example.hello_world.train_sql;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.hello_world.R;

public class train_Sql01 extends AppCompatActivity implements View.OnClickListener {

    private Button Btn_insert,Btn_query,Btn_update,Btn_delete;
    private train01_OpenHelper helper;
    private  SQLiteDatabase db;
    private StringBuilder sb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train__sql01);
        Btn_insert=findViewById(R.id.insert);
        Btn_query=findViewById(R.id.query);
        Btn_update=findViewById(R.id.update);
        Btn_delete=findViewById(R.id.delete);
        helper=new train01_OpenHelper(this,"student01.db",null,1);


        Btn_insert.setOnClickListener(this);
        Btn_query.setOnClickListener(this);
        Btn_update.setOnClickListener(this);
        Btn_delete.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        db=helper.getWritableDatabase();
        switch (v.getId()) {
            case R.id.insert:
                ContentValues values=new ContentValues();
                values.put("name","朱艳霞");
                db.insert("trainsql01",null,values);
                Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.query:
                sb=new StringBuilder();
                Cursor cursor=db.query("trainsql01",null,null,null,null,null,null);
                if (cursor.moveToFirst()) {
                    do{
                        String name=cursor.getString(cursor.getColumnIndex("name"));
                        int id=cursor.getInt(cursor.getColumnIndex("studentId"));
                        sb.append("id:"+id+"  "+"name:"+name);
                    }while(cursor.moveToNext());
                }
                cursor.close();
                Toast.makeText(this, sb, Toast.LENGTH_SHORT).show();
                break;

            case R.id.update:
                ContentValues values2=new ContentValues();
                values2.put("name","漩涡鸣人");
                db.update("trainsql01",values2,"name=?",new String[]{"朱艳霞"});
                Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
                break;

            case R.id.delete:
                db.delete("trainsql01","name=?",new String[]{"朱艳霞"});
                break;
        }
    }
}